package com.dyoud.api.shop.service;

import java.util.Date;
import java.util.List;

import org.douyoudian.utils.IdUtils;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dyoud.api.shop.employee.dto.AgreementDto;
import com.dyoud.common.api.model.Agreement;
import com.dyoud.common.api.service.AgreementService;
import com.dyoud.core.DozerUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/***
 * 
 * @author wangshuaifei
 * @date 2018/03/03
 *
 */
@Service
public class AgreementApiService {

    @Reference
    public AgreementService agreementService;

    /**
     * 添加协议
     * 
     * @param agreement
     * @return
     */
    public boolean addAgreement(AgreementDto dto) {
        Agreement agreement = new Agreement();
        DozerUtils.copy(dto, agreement);
        agreement.setCreateTime(new Date());
        agreement.setAgreementId(IdUtils.genId());
        return agreementService.addAgreement(agreement) > 0;
    }

    /**
     * 修改协议
     * 
     * @param agreement
     * @return
     */
    public boolean updateAgreement(AgreementDto dto) {
        Agreement agreement = new Agreement();
        DozerUtils.copy(dto, agreement);
        return agreementService.updateAgreement(agreement) > 0;
    }

    /**
     * 获取协议
     * 
     * @param agreementKey
     * @return
     */
    public AgreementDto getAgreement(String agreementKey) {
        Agreement agreement = agreementService.getAgreement(agreementKey);
        if(agreement == null) {
            return null;
        }
        AgreementDto dto = new AgreementDto();
        DozerUtils.copy(agreement, dto);
        return dto;
    }

    /**
     * 分页获取所有协议
     * 
     * @param agreementKey
     * @return
     */
    public PageInfo<Agreement> getAllAgreement(Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Agreement> list = agreementService.selectAgreement();
        return new PageInfo<Agreement>(list);

    }
    
}
